package com.qf.agro.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.mapper.SysUserMapper;
import com.qf.agro.pojo.SysUser;
import com.qf.agro.service.SysUserService;
import com.qf.agro.utils.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ProjectName: smart-agriculture-parent
 * ClassName: SysServiceImpl
 * Author: chenyaosen
 * Date: 2023/6/29 16:25
 * Description:
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public ResultInfo login(String username, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(username != null, SysUser::getUsername, username);
        SysUser login = userMapper.selectOne(queryWrapper);
        if (login == null) {
            return ResultInfo.error("用户名或密码错误");
        }
        if (!StringUtils.equals(login.getPassword(), SecureUtil.md5(password))) {
            return ResultInfo.error("用户名或密码错误");
        }
        String token = jwtUtil.createJWT(login.getId().toString(), JSON.toJSONString(login), null);
        return ResultInfo.success(token);
    }

    @Override
    public Page<SysUser> findPage(Integer pageNum, Integer pageSize, String name) {
        Page<SysUser> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, SysUser::getUsername,name);
        page = userMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public void save(SysUser sysUser) {
        sysUser.setPassword(SecureUtil.md5("qf1234"));
        sysUser.setState(1);
        sysUser.setGroupId(1l);
        sysUser.setManagerId(1l);
        //sysUser.setCompanyId(1l);
        //sysUser.setCompanyName("千锋互联");
        //sysUser.setCreateBy(1l);
        //sysUser.setUpdateBy(1l);
        //sysUser.setCreateTime(new Date());
        //sysUser.setUpdateTime(new Date());
        userMapper.insert(sysUser);
    }

    @Override
    public Boolean checkUsername(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(username!=null, SysUser::getUsername,username);
        List<SysUser> sysUsers = userMapper.selectList(queryWrapper);
        if (sysUsers == null||sysUsers.size() == 0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void disabled(Long id) {
        SysUser sysUser = new SysUser();
        sysUser.setState(0);
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId, id);
        userMapper.update(sysUser,queryWrapper);
    }

    @Override
    public void update(SysUser sysUser) {
        //sysUser.setUpdateBy(1l);
        //sysUser.setUpdateTime(new Date());
        userMapper.updateById(sysUser);
    }
}
